<?php
require_once ROOT_PATH . '/lib/models/surveys/Surveys.php';
require_once ROOT_PATH . '/lib/models/surveys/Options.php';
require_once ROOT_PATH . '/FirePHPCore/FirePHP.class.php';
$surveys = Surveys::getAll();
define("YES_NO",1);
define("SELECT_ONE",2);
define("SELECT_MULTI",3);
define("ANSWER",4);
if($records['question_id']==NULL){
	$action = 'AddQuestion';
	$disable='';
}
else{
	$action = 'EditQuestion';
	$disable='disabled';
} 
$survey = Surveys::getSurvey($records['survey_id']);
$surveytype = $survey->getSurveyType(); 	
?>
<script type="text/javascript" src="../../scripts/archive.js"></script>
<?php include ROOT_PATH."/lib/common/calendar.php"; ?>
<script type="text/javascript">
function addSave() {
	if(validateQuestion()=true)
		document.frmQuestion.submit();
	else
		return false;
}
function addOption() {
		oLayer = document.getElementById("addAdminLayer").style.display = 'block';
}
function saveOptions() {
	if(validateOption())
	     document.frmOptionAdmins.submit();
	else
		return false;
}
function removeOption() {
		var check = false;
		with (document.frmOptionAdmins) {
			for (var i=0; i < elements.length; i++) {
				if ((elements[i].type == 'checkbox') && (elements[i].checked == true) && (elements[i].id == 'chkID')){
					check = true;
					break;
				}
			}

			if (check) {
				document.frmOptionAdmins.action= "<?php echo $_SERVER['PHP_SELF']; ?>?surveycode=Surveys&action=DeleteOptions";
				document.frmOptionAdmins.submit();
			} else {
				alert("<?php echo $lang_Error_SelectAtLeastOneRecordToDelete; ?>");
			}
		}
}
function validateQuestion() {
	var errFlag=false;
	var question = document.getElementById('txtQuestion').value;
	if(question.length ==0){
		alert(" Question can not be null!");
	}else{
		errFlag = true;
	}
	return errFlag;
}

function validateOption() {
	var errFlag=false;
	var option = document.getElementById('txtOption').value;
	if(option.length ==0){
		alert(" Option can not be null!");
	}else{
		errFlag = true;
	}
	return errFlag;
}

</script>
<h2>
<?php echo $lang_Question_Edit; ?>
<hr/>
</h2>
<div id="status"></div>
<?php if (isset($_GET['message'])) {
		
		$expString  = $_GET['message'];
		
		//$col_def = CommonFunctions::getCssClassForMessage($expString);
		//$expString = '$lang_Common_' . $expString;
		
?>
		<font class="<?php //echo $col_def?>" size="-1" face="Verdana, Arial, Helvetica, sans-serif">
<?php echo $expString; ?>
		</font>
<?php }	?>
<form name="frmQuestion" id="frmQuestion" method="post"  action="<?php echo $_SERVER['PHP_SELF']; ?>?surveycode=Surveys&action=EditQuestion">
<input type="hidden" name="txtId" value="<?php echo $records['question_id']; ?>" >
<table border="0" cellpadding="5" cellspacing="0">
	<thead>
		<tr>
			<th class="tableTopLeft"></th>
	    	<th class="tableTopMiddle" ></th>
	    	<th class="tableTopMiddle" width="15"> </th>
	    	<th class="tableTopMiddle" width="5"></th>
	    	<th class="tableTopMiddle"></th>
			<th class="tableTopRight"></th>
		</tr>
	</thead>
	<tbody>
		
		<tr>
			<td class="tableMiddleLeft"></td>
			<td ><?php echo $lang_Question_Text; ?></td>			
			<td colspan="3">
				<TEXTAREA  id="txtQuestion" name="txtQuestion" value="" COLS="50" ROWS="2"/><?php echo $records['question_text'] ?></TEXTAREA>				
			</td>
			<td class="tableMiddleRight"></td>
		</tr>
		<tr>
			<td class="tableMiddleLeft"></td>
			<td ><?php echo $lang_Question_Surveys; ?></td>
			<td ><?php echo $survey->getSurveyName(); ?></td>
			<!-- 
			<td >
				<select name="availableList" id="availableList" size="5" multiple="multiple">
				
				</select>
				
			</td>			 
			<td valign="center">
				<a href="#" onclick="listbox_moveacross('availableList', 'currentList')">&gt;&gt;</a>
				<br>
				<a href="#" onclick="listbox_moveacross('currentList', 'availableList')">&lt;&lt;</a>
			</td>
			-->	
			<!-- 		
			<td >
				<select name="currentList"  id="currentList" size="1" READONLY="readonly" >
				
				<?php
					/*
					
					foreach($surveys as $survey){
						$selected = ""; 
						if($survey->getId()== $records['survey_id']){					
							$selected="SELECTED";
						}
					echo "<option ".$selected." value='".$survey->getId()."'>".$survey->getSurveyName()."</option>"					
				}
				 
					*/ ?>	
				</select>				
			</td>
			 -->
			<td></td>
			<td></td>
			<td class="tableMiddleRight"></td>
		</tr>
		<tr>
			<td class="tableMiddleLeft"></td>
			<td ><?php echo $lang_Question_Type; ?></td>
			
			<td >
				<select name="answerType" <?php echo $disable; ?> id="answerType" size="1"  >
					<?php for($i=1;$i<=5;$i++){
						$selected = "";
						if($i==1)$text = "Yes/No";
						elseif($i==2)$text="Select one";
						elseif ($i==3)$text="Multi Select";
						elseif ($i==4) $text="Type answer";
						elseif ($i==5)  $text="Quiz condition";
						if($i==$records['answer_type'])$selected="SELECTED";
						echo "<option ".$selected." value='".$i."' >".$text."</option>"; 
					} ?>							
				</select>
				
			</td>
			<td valign="center">
			</td>			
			<td >
			</td>
			<td class="tableMiddleRight"></td>
		</tr>
		<tr>
			<td class="tableMiddleLeft"></td>			
			<td >
				<input type="image"  onclick="return addSave();" name="btnSave" alt="Save"
					   src="../../themes/beyondT/pictures/btn_save.gif"
					/>
			</td>
			<td ></td>
			<td ></td>
			<td ></td>
			<td class="tableMiddleRight"></td>
		</tr>
		</tbody>
	<tfoot>
	  	<tr>
			<td class="tableBottomLeft"></td>
			<td class="tableBottomMiddle"></td>
			<td class="tableBottomMiddle"></td>
			<td class="tableBottomMiddle"></td>
			<td class="tableBottomMiddle"></td>
			<td class="tableBottomRight"></td>
		</tr>
  	</tfoot>
</table>
 </form>
 <form name="frmOptionAdmins" id="frmOptionAdmins" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?surveycode=Surveys&action=AddOption">
<?php if($records['answer_type']==2||$records['answer_type']==3 ||$records['answer_type']==5){ ?>
          	
			<input type="hidden" id="questionId" name="questionId" value="<?php echo $records['question_id']; ?>"/>
<table border="0" cellpadding="5" cellspacing="0">
	<thead>
		<tr>
			<th class="tableTopLeft"></th>
	    	<th class="tableTopMiddle" ></th>
	    	<th class="tableTopMiddle" width="15"> </th>
	    	<th class="tableTopMiddle" width="5"></th>
	    	<th class="tableTopMiddle"></th>
			<th class="tableTopRight"></th>
		</tr>
	</thead>
	<tbody>
		
		<tr>
			<td class="tableMiddleLeft"></td>			
			<td >
	
		
      <h3>Option list</h3>
       
      

		<?php
			$options = Options::getOptionsByQuestions($records['question_id']);
			if (!empty($options)) {
		?>

      <div >
		<table width="250" class="simpleList" >			
			<?php
				$odd = false;
				foreach ($options as $option) {
	 	 	 		$cssClass = ($odd) ? 'even' : 'odd';
	 	 	 		$odd = !$odd;
	 	 	 		if($option->getValue()==1){
	 	 	 			if($records['answer_type']==2){
	 	 	 				$checked ="checked='checked'";
	 	 	 				$typeInput = "type = 'radio'";
	 	 	 			}else{
	 	 	 				$checked ="checked='checked'";
	 	 	 				$typeInput = "type = 'checkbox'";
	 	 	 			}
	 	 	 		}else{
	 	 	 			if($records['answer_type']==2){
	 	 	 				$checked ="";
	 	 	 				$typeInput = "type = 'radio'";
	 	 	 			}else{
	 	 	 				$checked ="";
	 	 	 				$typeInput = "type = 'checkbox'";
	 	 	 			}
	 	 	 		}
	 		?>
    		<tr>
       			<td class="<?php echo $cssClass?>" align="center">
       				<input type='checkbox' class='checkbox' name='chkID[]' id='chkID'
       					value='<?php echo $option->getId();?>'></td>
		 		<td class="<?php echo $cssClass?>"><?php echo $option->getText(); ?></td>
		 		<?php if($surveytype == 2){ ?>
		 		<td class="<?php echo $cssClass?>"><input DISABLED name="txtValue" <?php echo $typeInput." ".$checked; ?> value = "<?php echo $option->getValue(); ?>"  ></td>
		 		<?php } ?>
			</tr>
		 	<?php
		 		}
		  	?>
 		</table>
		</div>
		 	<?php
			 }
		  	?>

			</br>
            <div align="left">
	            <img onClick="addOption()"
	            	onMouseOut="this.src='../../themes/beyondT/pictures/btn_add.gif';"
	            	onMouseOver="this.src='../../themes/beyondT/pictures/btn_add_02.gif';"
	            	src="../../themes/beyondT/pictures/btn_add.gif">
	        <?php
	        	if (!empty($options)) {
			?>
				<img
					onClick="removeOption()"
				    src="../../themes/beyondT/pictures/btn_delete.gif"
					onMouseOut="this.src='../../themes/beyondT/pictures/btn_delete.gif';"
					onMouseOver="this.src='../../themes/beyondT/pictures/btn_delete_02.gif';">
			<?php
				}
	        ?>
            </div>
       
			<div id ="addAdminLayer" style="display:none;height:20px;">
		    	<label for="projAdminName">New Option</label>
	               	<input type="text"  name="txtOption" id="txtOption" value="" >
	               	<?php if($records['answer_type']==2 && $surveytype == 2){ ?>
	               		<input type="radio" name="txtValue" id ="txtValue" value="1"/>
	               	<?php }else if($records['answer_type']==3 && $surveytype == 2){ ?>
	               		<input type="checkbox" name="txtValue" id ="txtValue" value="1"/>
	               	<?php } ?>	                  
	               
	            	<img onClick="return saveOptions();"
	            		style="margin-top:10px;"
	            		onMouseOut="this.src='../../themes/beyondT/icons/assign.gif';"
	            		onMouseOver="this.src='../../themes/beyondT/icons/assign_o.gif';"
	            		src="../../themes/beyondT/icons/assign.gif">
			</div>
     

			</td>
			<td ></td>
			<td ></td>
			<td ></td>
			<td class="tableMiddleRight"></td>
		</tr>
		
		
	  
	</tbody>
	<tfoot>
	  	<tr>
			<td class="tableBottomLeft"></td>
			<td class="tableBottomMiddle"></td>
			<td class="tableBottomMiddle"></td>
			<td class="tableBottomMiddle"></td>
			<td class="tableBottomMiddle"></td>
			<td class="tableBottomRight"></td>
		</tr>
  	</tfoot>
</table>
<?php }?>
 </form>
<div id="cal1Container" style="position:absolute;" ></div>
